package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 *
 *
 * 简历说明:
 * https://yt6m7m2sn5.feishu.cn/mindnotes/bmncnpM9X71JEtgmGekACw81vEg#mindmap
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

    @PostMapping(value = "/user-group")
    public Object userGroup(@RequestBody UserGroup userGroup){

        //写入到Mysql
        userGroupService.writeUserGroupToMysql(userGroup);

        //写入到ck
        userGroupService.writeUserGroupToCK(userGroup);

        //把ck中的人群包同步到redis一份
        UserGroup ug = userGroupService.writeUserGroupToRedis(userGroup);

        //把拥有人数的userGroup元数据再写入到mysql
        userGroupService.saveOrUpdate(ug);


        return "ok";

    }

    /*
            只进行计算，计算结果不写入mysql也不写入ck和redis
     */
    @PostMapping(value = "/user-group-evaluate")
    public Object evaluateUserGroupNum(@RequestBody UserGroup userGroup){

        Long num = userGroupService.evaluateUserGroupNum(userGroup);

        return num;

    }

    /*
        更新人群包.
            元数据： Mysql
            数据:   ck
                    redis
     */
    @PostMapping(value = "/user-group-refresh/{id}")
    public Object updateUserGroup(String busiDate,@PathVariable("id") String id){

        //根据id到mysql中查询元数据，知道人群包的计算条件是什么
        UserGroup userGroup = userGroupService.getById(id);

        //需要基于数据库查询的condition_json_str 转换为  List<TagCondition> tagConditions
        List<TagCondition> tagConditions = JSON.parseArray(userGroup.getConditionJsonStr(), TagCondition.class);

        userGroup.setTagConditions(tagConditions);

        System.out.println(userGroup);
        //赋值要计算的业务日期
        userGroup.setBusiDate(busiDate);

        //基于最新的业务日期重新计算
        // 更新ck  先删除之前旧的信息，再写入新的信息
        userGroupService.updateCK(userGroup);

        //更新redis
        UserGroup up = userGroupService.writeUserGroupToRedis(userGroup);

        //更新元数据
        up.setUpdateTime(new Date());

        userGroupService.saveOrUpdate(up);

        return "ok";


    }





}

